//-----------------------------------------------------------------------
// <copyright file="PublishedException.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics
{
    using System;

    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;

    using System.Security;
    using System.Security.Permissions;

    using System.Runtime.Serialization;

    /// <summary>
    /// Indicates that the <see cref="M:CodePlex.Diagnostics.PublishedException.InnerException"/> was published.
    /// </summary>
    [Serializable]
    public class PublishedException : Exception, IPublishedException
    {
        #region public PublishedException(Guid publicationId, Exception inner) : base(SR.GetString(SR.PublishedException), inner)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedException"/> class with the specified detailed description and the specified exception. 
        /// </summary>
        /// <param name="publicationId">The publication identifier of the inner exception.</param>
        /// <param name="inner">A reference to the inner exception that is the cause of this exception.</param>
        [SecurityPermission(SecurityAction.LinkDemand)]
        public PublishedException(Guid publicationId, Exception inner) : base(SR.GetString(SR.PublishedException), inner)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region protected PublishedException(SerializationInfo info, StreamingContext context) : base(info, context)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedException"/>  class using the specified serialization information and context objects.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is <c>null</c> or <see cref="P:System.Exception.HResult"/> is zero (0).</exception>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is <c>null</c>.</exception>
        [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
        protected PublishedException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            // retrieve the publication identifier.
            this.PublicationId = (Guid) info.GetValue(SR.PublicationId, typeof(Guid));
        }

        #endregion

        #region public Guid PublicationId

        /// <summary>
        /// Gets the publication Id associated with the <see cref="M:System.Exception.InnerException"/>.
        /// </summary>
        public Guid PublicationId
        {
            get;
            private set;
        }

        #endregion

        #region public override void GetObjectData(SerializationInfo info, StreamingContext context)

        /// <summary>
        /// Sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is a <c>null</c> reference (Nothing in Visual Basic). </exception>
        /// <PermissionSet>
        /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
        /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
        /// </PermissionSet>
        [SecurityCritical]
        [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            if (info != null)
            {
                // store the publication identifier.
                info.AddValue(SR.PublicationId, this.PublicationId);
            }
        }

        #endregion
    }
}

